import 'package:flutter/cupertino.dart';

import '../../model/page_status.dart';
import 'empty_indicator.dart';
import 'error_indicator.dart';
import 'loading_indicator.dart';

/// FileName page_indicator
///
/// @Author lax
/// @Date 2023/4/11
///
/// @Description

class PageIndicator extends StatelessWidget {
  /// 页面状态
  final PageStatus? pageStatus;

  /// 为true时展示暂无数据，默认null
  /// 为false且状态为error时，不显示error
  final bool? isEmpty;

  /// 状态为loading时是否显示child，默认false
  final bool loadingShowChild;

  /// 状态为error时是否显示child，默认false
  /// 如果有child且empty为false则不显示加载失败
  final bool errorShowChild;

  /// empty时是否显示child，默认false
  final bool emptyShowChild;

  /// 不为null时显示刷新按钮
  final VoidCallback? onRefresh;

  /// 不为null时显示重新加载按钮
  final VoidCallback? onReload;

  /// 状态为normal时展示child
  final Widget? child;

  final String? loadingText;
  final TextStyle? loadingTextStyle;

  final String? refreshText;
  final Widget? refreshWidget;

  final String? reloadText;
  final Widget? reloadWidget;

  final String? errorText;
  final TextStyle? errorTextStyle;
  final Widget? errorImage;

  final String? emptyText;
  final TextStyle? emptyTextStyle;
  final Widget? emptyImage;

  final double? width;
  final double? height;

  const PageIndicator({
    super.key,
    this.isEmpty,
    this.pageStatus = PageStatus.normal,
    this.loadingShowChild = false,
    this.errorShowChild = false,
    this.emptyShowChild = false,
    this.onRefresh,
    this.onReload,
    this.child,
    this.loadingText,
    this.loadingTextStyle,
    this.refreshText,
    this.refreshWidget,
    this.reloadText,
    this.reloadWidget,
    this.errorText,
    this.errorTextStyle,
    this.errorImage,
    this.emptyText,
    this.emptyTextStyle,
    this.emptyImage,
    this.width,
    this.height,
  });

  @override
  Widget build(BuildContext context) {
    switch (pageStatus) {
      case PageStatus.loading:
        {
          return Stack(
            children: [
              Visibility(
                visible: loadingShowChild,
                child: child ?? const SizedBox.shrink(),
              ),
              LoadingIndicator(
                text: loadingText,
                textStyle: loadingTextStyle,
                width: width,
                height: height,
              ),
            ],
          );
        }
      case PageStatus.error:
        {
          return Stack(
            children: [
              Visibility(
                visible: !(errorShowChild && isEmpty == false),
                child: ErrorIndicator(
                  text: errorText,
                  textStyle: errorTextStyle,
                  image: errorImage,
                  onReload: onReload,
                  reloadText: reloadText,
                  reloadWidget: reloadWidget,
                  width: width,
                  height: height,
                ),
              ),
              Visibility(
                visible: errorShowChild,
                child: child ?? const SizedBox.shrink(),
              ),
            ],
          );
        }
      case PageStatus.normal:
        {
          return Stack(
            children: [
              Visibility(
                visible: isEmpty == true,
                child: EmptyIndicator(
                  text: emptyText,
                  textStyle: emptyTextStyle,
                  image: emptyImage,
                  onRefresh: onRefresh,
                  refreshText: refreshText,
                  refreshWidget: reloadWidget,
                  width: width,
                  height: height,
                ),
              ),
              Visibility(
                visible: isEmpty != true || emptyShowChild,
                child: child ?? const SizedBox.shrink(),
              ),
            ],
          );
        }
      default:
        return const SizedBox.shrink();
    }
  }
}
